package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import models.ActionItem;
import models.ActionLog;
import models.Comment;
import models.EEntity;
import models.Idea;
import models.Notification;
import models.Organization;
import models.Topic;
import models.User;

import play.mvc.Controller;
import play.mvc.Router;
import play.mvc.With;

/**
 * 
 * @author Abdalla Abd-alhady
 * @version 1.0
 * 
 */
@With(Secure.class)
public class NotificationController extends Controller {

	/**
	 * the method redirects the user to his notification page.
	 * 
	 * @author Abdalla Abd-alhady
	 */
	public static void viewNotification() {

		User user = User.getUserInfo(session.get("username"));
		if (user == null) {
			render("Application/notLoggedIn.html");
		}
		notFoundIfNull(user);
		List<Notification> tempNotifications = user.notifications;
		List<Notification> notifications = new LinkedList<Notification>();
		for (int i = tempNotifications.size() - 1; i != -1; i--) {
			notifications.add(tempNotifications.get(i));
		}
		render("Browse/viewNotification.html", notifications);
	}

	/**
	 * this method is for redirecting the user if he click on one of the
	 * notification
	 * 
	 * @author Abdalla Abd-alhady
	 * 
	 * @param objectID
	 *            the object id (like : comment.id , topic.id ...etc)
	 * @param typeID
	 *            the type of the object (comment , topic ,idea ..etc)
	 */
	public static void gotoNotification(long objectID, int typeID) {

		switch (typeID) {
		case 0:
			ActionItem actionItem = ActionItem.findById(objectID);
			Application.showPlan(actionItem.plan.id);
			break;
		case 2:
			Comment comment = Comment.findById(objectID);
			Browse.viewIdea(comment.idea.id);
			break;
		case 3:
			Browse.viewEntity(objectID);
			break;
		case 4:
			Browse.viewIdea(objectID);
			break;
		case 6:
			Browse.viewOrganization(objectID);
			break;
		case 8:
			Application.showPlan(objectID);
			break;
		case 11:
			Browse.viewTopic((objectID));
			break;
		case 13:
			ProfileController.myMergeRequests();
			break;
		case 15:
			Users.showWall();
			break;
		case 18:
			Browse.viewRequests(objectID);
			break;
		}
	}

	/**
	 * the method report an idea.
	 * 
	 * @author Abdalla Abd-alhady
	 * 
	 * @param idIdea
	 *            the id of the idea that been reported
	 * @param reportDescr
	 *            the report message
	 * @param reason
	 *            the main reason for reporting.
	 * 
	 */
	public static void reportIdea(long idIdea, String reportDescr, String reason) {

		if (!Security.isConnected()) {
			render("Users/register.html");
		}
		validation.required(reportDescr).message(
				"please enter report message!!");
		if (validation.hasErrors()) {
			params.flash();
			validation.keep();
			viewReportPageIdea(idIdea);
		} else {
			User reporter = User.getUserInfo(session.get("username"));
			if (reporter == null) {
				render("Application/notLoggedIn.html");
			}
			Idea idea = Idea.findById(idIdea);
			notFoundIfNull(idea);
			Organization Org = idea.topic.entity.organization;
			if (Org.privacyStatus == 0 || Org.isMember(reporter.id)) {
				Application.sendMail(reportDescr, "Report",
						idea.topic.entity.getOrganizers());
				new Notification("Report reason :" + reason
						+ " Report message:" + reportDescr, idIdea, 4,
						reporter, idea.topic.entity.getOrganizers()).save();
				String Msg = "User name " + reporter.name + " reported an idea"
						+ idea.title;
				new ActionLog(Msg, reporter, null).save();
			} else {
				notFound();
			}
			int flag = 1;
			render("Application/reportSent.html", idIdea, flag);
		}

	}

	/**
	 * the method is for redirecting to the report form page.
	 * 
	 * @param idIdea
	 *            the id of the reported idea
	 * 
	 * @author Abdalla Abd-alhady
	 */
	public static void viewReportPageIdea(long idIdea) {
		int flag = 1;
		render("Application/reportForm.html", idIdea, flag);
	}

	/**
	 * the method is for redirecting to the report form page.
	 * 
	 * @story C3S44
	 * @author Abdalla Abd-alhady
	 * 
	 * @param idTopic
	 *            the id of the reported topic.
	 */
	public static void viewReportPageTopic(long idTopic) {
		int flag = 2;
		render("Application/reportForm.html", idTopic, flag);
	}

	/**
	 * the method is for redirecting to the report form page.
	 * 
	 * @story C3S44
	 * @author Abdalla Abd-alhady
	 * 
	 * @param idComment
	 *            the id of the reported comment.
	 */
	public static void viewReportPageComment(long idComment) {
		int flag = 3;
		render("Application/reportForm.html", idComment, flag);
	}

	/**
	 * this method is used by Ajax call done to retrieve The most recent
	 * notifications
	 * 
	 * @author Ahmed Mahmoud
	 */
	public static void getMoreNotifications() {
		List<Notification> notifications = Notification.find("timeStamp > ?",
				request.date).fetch();
		if (notifications.isEmpty()) {
			suspend("1mn");
		}
		String result = "";
		for (Notification notification : notifications) {
			Map paramtersMap = new HashMap<String, Integer>();
			paramtersMap.put("objectID", notification.objectID);
			paramtersMap.put("objectType", notification.objectType);
			String url = Router.reverse(
					"Notificationscontroller.gotoNotification", paramtersMap).url;
			result += "<span class=\"user\"> User name:"
					+ notification.notifictionTrigger.userName + "</span>"
					+ "<a href=" + url + ">"
					+ "<span class=\"description\">----- Description: "
					+ notification.description + "		</span></a>"
					+ "<span class=\"dateTime\"> Time: "
					+ notification.timeStamp + " 	<br><br>		</span>";

		}
		renderText(result);
	}

	/**
	 * the method is for reporting topic
	 * 
	 * @story C3S44
	 * @author Abdalla Abd-alhady
	 * 
	 * @param idTopic
	 *            the id of the reported topic.
	 * @param reportDescr
	 *            the report message.
	 * @param reason
	 *            the main reason for reporting.
	 */
	public static void reportTopic(long idTopic, String reportDescr,
			String reason) {
		if (!Security.isConnected()) {
			render("Users/register.html");
		}
		validation.required(reportDescr).message(
				"please enter report message!!");
		if (validation.hasErrors()) {
			params.flash();
			validation.keep();
			viewReportPageTopic(idTopic);
		} else {
			User reporter = User.getUserInfo(session.get("username"));
			if (reporter == null) {
				render("Application/notLoggedIn.html");
			}
			Topic topic = Topic.findById(idTopic);
			notFoundIfNull(topic);
			Organization Org = topic.entity.organization;
			if (Org.privacyStatus == 0 || Org.isMember(reporter.id)) {
				Application.sendMail(reportDescr, "Report",
						topic.entity.getOrganizers());
				new Notification("Report reason :" + reason
						+ " Report message:" + reportDescr, idTopic, 11,
						reporter, topic.entity.getOrganizers()).save();
				String Msg = "User name " + reporter.name
						+ " reported an comment" + topic.name;
				new ActionLog(Msg, reporter, null).save();
			} else {
				notFound();
			}
			int flag = 2;
			render("Application/reportSent.html", idTopic, flag);
		}

	}

	/**
	 * the method is for reporting the comments.
	 * 
	 * @story C3S44
	 * @author Abdalla Abd-alhady
	 * 
	 * @param idComment
	 *            the id of the reported comment.
	 * @param reportDescr
	 *            the report message.
	 * @param reason
	 *            the main reason for reporting.
	 */
	public static void reportComment(long idComment, String reportDescr,
			String reason) {

		if (!Security.isConnected()) {
			render("Users/register.html");
		}
		validation.required(reportDescr).message(
				"please enter report message!!");
		if (validation.hasErrors()) {
			params.flash();
			validation.keep();
			viewReportPageComment(idComment);
		} else {
			User reporter = User.getUserInfo(session.get("username"));
			if (reporter == null) {
				render("Application/notLoggedIn.html");
			}
			Comment comment = Comment.findById(idComment);
			notFoundIfNull(comment);
			Organization Org = comment.idea.topic.entity.organization;
			if (Org.privacyStatus == 0 || Org.isMember(reporter.id)) {
				Application.sendMail(reportDescr, "Report",
						comment.idea.topic.entity.getOrganizers());
				new Notification("Report reason :" + reason
						+ " Report message:" + reportDescr, idComment, 2,
						reporter, comment.idea.topic.entity.getOrganizers())
						.save();
				String Msg = "User name " + reporter.name
						+ " reported an comment" + comment.content;
				new ActionLog(Msg, reporter, null).save();
			} else {
				notFound();
			}
			int flag = 3;
			long idIdea = comment.idea.id;
			render("Application/reportSent.html", idIdea, flag);
		}

	}
}
